package tq.com.tqteacher.activity.newPublish;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.githang.statusbar.StatusBarCompat;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import tq.com.systools.system.AlertDialogHelper;
import tq.com.systools.system.StringHelper;
import tq.com.tqcom.base.BaseActivity;
import tq.com.tqcom.base.BaseFragment;
import tq.com.tqcom.base.BaseRecycleActivity;
import tq.com.tqcom.base.BaseRecyclerFragment;
import tq.com.tqcom.base.BaseTitleActivity;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.net.TQPictureUpload;
import tq.com.tqcom.util.TQLoadingHelper;
import tq.com.tqcom.view.TQHeader5View;
import tq.com.tqteacher.R;
import tq.com.tqteacher.activity.TQPdfReaderActivity;
import tq.com.tqteacher.activity.captureVideo.TQCaptureActivity;
import tq.com.tqteacher.activity.playerView.TQPlayerActivity;
import tq.com.tqteacher.adapt.newPublish.TQWKListAdapter;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQLogin;
import tq.com.tqteacher.model.TQNewPublish;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.model.TQWKPublish;
import tq.com.tqteacher.net.TQNetInterface;
import tq.com.tqteacher.view.TQHeader3ViewHold;
import tq.com.upload.IUploadFileListener;
import tq.com.upload.TQUploadFileManager;

public class TQWKListActivity extends BaseTitleActivity {
    private TQWKListFragment mTQWKListFragment = new TQWKListFragment();

    @Override
    protected BaseFragment fragment() {
        return mTQWKListFragment;
    }

    @Override
    protected void initHeader(TQHeader5View pHeader5View) {
        super.initHeader(pHeader5View);
        pHeader5View.getTitleView().setText("微课");
    }

    static public class TQWKListFragment extends BaseFragment{
        private TQWKListAdapter mTQWKListAdapter;
        private RecyclerView mRecyclerView;
        private SmartRefreshLayout mRefreshLayout;
        private TQAtachModel mTQAtachModel;

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            View lView = inflater.inflate(R.layout.activity_wk_list_layout, container, false);
            return lView;
        }

        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            mRecyclerView = view.findViewById(R.id.list);
            if(mTQWKListAdapter == null){
                mTQWKListAdapter = new TQWKListAdapter(getContext());
                mTQWKListAdapter.setIClickListener(mClickListener);
            }

            mRecyclerView.setAdapter(mTQWKListAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

            view.findViewById(R.id.new_wk).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    next();
                }
            });

            mRefreshLayout = view.findViewById(R.id.refresh);
            mRefreshLayout.setEnableLoadMore(false);
            mRefreshLayout.setEnableRefresh(false);

            TQWKPublish lTQWKPublish = (TQWKPublish)TQPublishType.getSharePublishItem();
            if(lTQWKPublish.isPublish() && TQPublishType.getTQOpenFrom() == TQPublishType.TQOpenFrom.home){
                view.findViewById(R.id.new_wk).setVisibility(View.GONE);
                mTQWKListAdapter.setCanModify(false);
            }else{
                if(lTQWKPublish.getVideoSetsModels().size() == 0){
                    next();
                }
            }


        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(resultCode != BaseActivity.RESULT_OK){
                return;
            }
            if(requestCode == 0x06){
                String path = data.getStringExtra(TQCaptureActivity.RESULT_KEY);
//                mTQAtachModel.replaceFile(path);
//
//                moidfyCapture();
                uploadFile(path);
            }
            super.onActivityResult(requestCode, resultCode, data);

        }

        @Override
        public void onStart() {
            super.onStart();

            TQWKPublish lTQWKPublish = (TQWKPublish)TQPublishType.getSharePublishItem();
            mTQWKListAdapter.replaceData(lTQWKPublish.getVideoSetsModels());
        }

        private void next(){
            Intent lIntent = new Intent(this.getContext(), TQWKCaptureTypeActivity.class);
            startActivity(lIntent);
        }

        private TQWKListAdapter.IClickListener mClickListener = new TQWKListAdapter.IClickListener() {
            @Override
            public void play(TQAtachModel pModel) {
                String url = TQPictureUpload.downloadPath(pModel.getFirstFile());
                TQPlayerActivity.showPlayer(url, TQWKListFragment.this.getActivity());
            }

            @Override
            public void deleteAttach(final TQAtachModel pModel) {
                AlertDialogHelper.show(TQWKListFragment.this.getContext(), "确定删除?", new AlertDialogHelper.ISumbimtListener() {
                    @Override
                    public void ok() {
                        delAttach(pModel);
                    }
                });
            }

            @Override
            public void recapture(TQAtachModel pModel) {
                mTQAtachModel = pModel;
                if(mTQAtachModel == null) return;

                String url = null;
                if(!StringHelper.isEmpty(pModel.getCover())){
                    url = TQPictureUpload.downloadPath(pModel.getCover());
                }

                TQCaptureActivity.requestCapture(url, TQWKListFragment.this, 0x06);
            }
        };

        private void moidfyCapture(){
            if(mTQAtachModel == null) return;
            TQLoadingHelper.build(this.getContext()).show();
            mTQAtachModel.doModifyAttach(new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()) {
                        TQLoadingHelper.setError(pNetResult.getErrMsg());
                        return;
                    }

                    TQLoadingHelper.dimiss();
                }
            });
        }

        private void delAttach(final TQAtachModel pModel){
            if(pModel == null) return;
            TQLoadingHelper.build(this.getContext()).setCancelable(false).show();

            TQNetInterface.delMaterialAttrach(pModel.getId(), new INetCallback() {
                @Override
                public void result(TQNetResult pNetResult) {
                    if(!pNetResult.isSuccess()){
                        TQLoadingHelper.setError(pNetResult.getErrMsg());
                        return;
                    }

                    TQWKPublish lTQWKPublish = (TQWKPublish) TQPublishType.getSharePublishItem();
                    lTQWKPublish.delAttach(pModel);
                    mTQWKListAdapter.replaceData(lTQWKPublish.getVideoSetsModels());
                    TQLoadingHelper.dimiss();
                }
            });
        }

        private void uploadFile(String file){
            if(file == null)return;
            TQLoadingHelper.build(this.getContext()).setCancelable(false).show();
            TQUploadFileManager.Build lBuild = new TQUploadFileManager.Build()
                    .setFile(file)
                    .setToken(TQLogin.getCurrent().getToken())
                    .setListener(new IUploadFileListener() {
                        @Override
                        public void process(long current, long total) {
                            Log.d(TAG, "process: ");
                            TQLoadingHelper.setText(String.format("进度%d%%", (int)(current/(total*1.0f) * 100)));
                        }

                        @Override
                        public void fail(String msg) {
                            Log.d(TAG, "fail: ");
                            TQLoadingHelper.setError(msg);
                        }

                        @Override
                        public void success(String filePath) {
                            Log.d(TAG, "success: ");
                            TQLoadingHelper.dimiss();;

                            mTQAtachModel.replaceFile(filePath);
                            moidfyCapture();
                        }
                    });

            lBuild.start();

        }
    }
}

